package nats_controller

import (
	"gluenet/internal/glue-srvp/domain"
	"gluenet/internal/glue-srvp/domain/vo"
	"gluenet/internal/pkg/infra"
	"gluenet/internal/pkg/infra/prepare"
)

func init() {
	prepare.RegisterNatsController(func() {
		agent := NewAgent()
		prepare.InjectNatsController(func() *Agent {
			return agent
		})
	})
}

type Agent struct {
	Agent *domain.Agent
}

// Nats serves
// Input: null
// Topic: v2.rpc.apis.agent.topics
func (r *Agent) Topics() *infra.Response {
	return infra.ResponseOK(infra.ListNatRouter())
}

// Nats serves
// Input: json bts (vo.AgentRegisterReq)
// Topic: v2.rpc.apis.agent.regsiter
func (r *Agent) Regsiter(req vo.AgentRegisterReq) *infra.Response {
	res, err := r.Agent.Register(req)

	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(res)
}

// Nats serves
// Input: string
// Topic: v2.rpc.apis.agent.unregsiter
func (r *Agent) UnRegsiter(guid string) *infra.Response {
	err := r.Agent.UnRegister(guid)
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(nil)
}

// Nats serves
// Input: string
// Topic: v2.rpc.apis.agent.getconfig
func (r *Agent) GetConfig(guid string) *infra.Response {
	res, err := r.Agent.GetConfig(guid)
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(res)
}

// Nats serves
// Topic: v2.rpc.apis.guid.putconfig
func (r *Agent) PutConfig(req vo.AgentPutConfigReq) *infra.Response {
	err := r.Agent.PutConfig(req)
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(nil)
}

func NewAgent() *Agent {
	r := &Agent{}
	prepare.FetchDependency(&r.Agent)
	return r
}
